package com.bootcamp.servlet.cart;

/**
 * @program: wrok-1101
 * @author: Keyu Li
 * @create: 2021-11-04 19:50
 **/

import com.bootcamp.servlet.mvc.model.Product;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ShowCartServlet", value = "/ShowCartServlet")
public class ShowCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        PrintWriter writer = response.getWriter();
        // 如果session为null或者未存储购物车，则跳转回首页
        Object o;
        String storepage = request.getContextPath() + "/ShopStoreServlet";
        if (session == null || (o = session.getAttribute("cartItem")) == null) {
            writer.println("购物车为空！正在跳转购物页面ing");
            response.setHeader("refresh", "1;url=" + storepage);
            return;
        }
        // 获取全部商品信息，及购物车信息
        Map<Integer, Integer> cartItems = (Map<Integer, Integer>) o;
        List<Product> products = (List<Product>) getServletContext().getAttribute("products");
        // 返回首页
        writer.println("<div><a href='" + storepage + "'>返回购物页面</a></div>");
        // 展示商品
        writer.println("<div><table>");
        // 第一行
        writer.println("        <tr style=\"font-size: larger\">\n" +
                "            <td>品牌</td>\n" +
                "            <td>颜色</td>\n" +
                "            <td>类别</td>\n" +
                "            <td>价格</td>\n" +
                "            <td>描述</td>\n" +
                "            <td>详情</td>\n" +
                "            <td>数量</td>\n" +
                "            <td></td>\n" +
                "        </tr>");
        // 具体商品内容，同时计算商品价格
        BigDecimal total = new BigDecimal("0");
        for (Product p : products) {
            Integer count = cartItems.get(p.getId());
            if (count == null || count == 0)
                continue;
            if (count < 0) {
                count = 0;
                continue;
            }
            // 输出商品详情
            String detailhref = request.getContextPath() + "/GoodsDetailServlet?id=" + p.getId();
            writer.println("<tr>\n" +
                    "                <td>" + p.getName() + "</td>\n" +
                    "                <td>" + p.getColor() + "</td>\n" +
                    "                <td>" + p.getCategory() + "</td>\n" +
                    "                <td>" + p.getPrice() + "</td>\n" +
                    "                <td>" + p.getDescription() + "</td>\n" +
                    "                <td><a href='" + detailhref + "'>详情</a></td>\n" +
                    "                <td>" + count + "</td>\n" +
                    "        </tr>");
            // 计算总结
            total = total.add(p.getPrice().multiply(new BigDecimal(count)));
        }
        // 结算
        writer.println("<tr style='font-size: larger'><td>总价：</td><td>" + total + "</td></tr>");
        writer.println("<tr><td><a href='https://www.taobao.com'>结算</a></td></tr>");
        writer.println("</table></div>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
